//
//  DYCommonCell.m
//  PrivatePhoto
//
//  Created by PrivatePhoto Team on 2021.
//  Copyright © 2021年 PrivatePhoto Team. All rights reserved.
//

#import "DYCommonCell.h"
@interface DYCommonCell ()

@property (nonatomic, strong) UIButton *seletedBtn;
@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *detailLabel;
@property (nonatomic, strong) UIImageView *accImg;

@property (nonatomic, strong) WKClearPhotoItem *item;

@property (nonatomic, strong) CAPhotoScanInfo *model;

@end

@implementation DYCommonCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.contentView.backgroundColor = [UIColor colorWithHexString:@"#EDF4FF"];
        
        [self configSubView];
    }
    return self;
}

-(void)setFrame:(CGRect)frame
{
    frame.origin.x = 10;//这里间距为10，可以根据自己的情况调整
    frame.size.width -= 2 * frame.origin.x;
    frame.size.height -= 2 * 5;
    [super setFrame:frame];
    
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = 12.0;
    UIImage *bk = [UIImage imageNamed:@"bk"];
    [self.seletedBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(10);
        make.centerY.mas_offset(0);
        make.width.height.mas_equalTo(bk.size.width);
    }];
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(40);
        make.top.bottom.right.mas_offset(0);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.containerView.mas_left);
        make.top.mas_offset(10);
    }];
    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.containerView.mas_left);
        make.top.mas_equalTo(self.nameLabel.mas_bottom);
        make.bottom.mas_offset(-5);
    }];
    [self.accImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-10);
        make.centerY.mas_offset(0);
    }];
    
    
}

- (void)configSubView{
    [self.contentView addSubview:self.seletedBtn];
    [self.contentView addSubview:self.containerView];
    [self.containerView addSubview:self.nameLabel];
    [self.containerView addSubview:self.detailLabel];
    [self.containerView addSubview:self.accImg];
}

- (void)btnDidClick:(UIButton *)sender{
    sender.selected = !sender.selected;
    self.item.isSeletedItem = sender.selected;
    !self.seletedBtnBlock ?:self.seletedBtnBlock(self.item);
    
    self.model.is_seleted = sender.selected;
    !self.seletedCompleteBlock ?:self.seletedCompleteBlock(self.model);
}

- (void)bindWithMode:(WKClearPhotoItem *)item {
    self.item = item;
    self.seletedBtn.selected = item.isSeletedItem;
    self.nameLabel.text = item.name;
    self.detailLabel.text = [NSString stringWithFormat:@"%@ . %@", item.detail, item.saveStr];
}

- (void)bindWithInfo:(CAPhotoScanInfo *)item{
    self.model = item;
    self.seletedBtn.selected = item.is_seleted;
    self.nameLabel.text = [self nameForType:item.cleanType];
    self.detailLabel.text = [NSString stringWithFormat:@"%ld张 . %.2fMB", (long)item.count, (unsigned long)item.saveSpace/1024.0/1024.0];
}

- (NSString *)nameForType:(CAClearAlbumType)type{
    NSString *name = @"";
    switch (type) {
        case CAClearAlbumTypeSimilar:
            name = @"相似照片处理";
            break;
        case CAClearAlbumTypeScreenshots:
            name = @"截屏照片清理";
            break;
        case CAClearAlbumTypeSamePhoto:
            name = @"重复照片";
            break;
        case CAClearAlbumTypeLivePhoto:
            name = @"类似动态照片";
            break;
        case CAClearAlbumTypeVideo:
            name = @"相似视频";
            break;
        default:
            break;
    }
    return name;
}


#pragma mark --setter/getter

- (UIView *)containerView{
    if (!_containerView) {
        _containerView = [[UIView alloc] init];
    }
    return _containerView;
}

- (UIImageView *)accImg{
    if (!_accImg) {
        _accImg = [[UIImageView alloc] init];
        _accImg.image = [UIImage imageNamed:@"jt"];
    }
    return _accImg;
}

- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.font = [UIFont systemFontOfSize:16 weight:3];
        _nameLabel.textColor = [UIColor colorWithHexString:@"#323232"];
        _nameLabel.text = @"屏幕截图";
    }
    return _nameLabel;
}

- (UILabel *)detailLabel{
    if (!_detailLabel) {
        _detailLabel = [[UILabel alloc] init];
        _detailLabel.font = [UIFont systemFontOfSize:14];
        _detailLabel.textColor = [UIColor colorWithHexString:@"#323232"];
        _detailLabel.text = @"430张照片  677.2MB";
    }
    return _detailLabel;
}

- (UIButton *)seletedBtn{
    if (!_seletedBtn) {
        _seletedBtn = [[UIButton alloc] init];
        [_seletedBtn setImage:[UIImage imageNamed:@"xz"] forState:UIControlStateSelected];
        [_seletedBtn setImage:[UIImage imageNamed:@"bk"] forState:UIControlStateNormal];
        [_seletedBtn addTarget:self action:@selector(btnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _seletedBtn;
}

@end
